﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.cms.businesslogic.member;
using umbraco.BusinessLogic;
using PBDesk.Utilities;

namespace PBDesk.Umbraco.Helper
{
    public class UserManager
    {
        public const string FACEBOOK_PROVIDER = "facebook";
        public const string FACEBOOK_PREFIX = "fb_";
        public static MemberType GetSubscriberMemberType()
        {
            return MemberType.GetByAlias("Subscriber");
        }

        public static Member CreateFacebookSubscriber(string fbUserId, string fbUserName, string gender, string location)
        {

            try
            {
                if (fbUserId.IsNullOrEmpty())
                {
                    throw new Exception("Error in CreateFacebookSubscriber. Facebook user id is null or empty");
                }
                

                if (!Member.IsMember(fbUserId))
                {

                    if (fbUserName.IsNullOrEmpty())
                        fbUserName = "Facebook User";
                    else
                        fbUserName = fbUserName.Trim();

                    if (gender.IsNullOrEmpty())
                        gender = "N";
                    else
                        if (gender.Trim().ToLower().Contains("fe"))
                            gender = "F";
                        else
                            gender = "M";

                    if (location == null)
                        location = string.Empty;
                    else
                        location = location.Trim();

                    string email = fbUserName + "@facebook.fb";


                    return CreateSubscriber(fbUserId, fbUserName, email, FACEBOOK_PROVIDER, gender, location);
                }               
            }
            catch (Exception ex)
            {
                UmbHelper.WriteExceptionLog("UserManager.CreateFacebookSubscriber()", ex);
            }
            return null;
        }

        internal static Member CreateSubscriber(string loginName, string userName, string email, string provider, string gender, string locaction)
        {
            try
            {
                
                MemberType subscriber = GetSubscriberMemberType();
                Member newSubscriber = Member.MakeNew(userName, loginName, email, subscriber, new umbraco.BusinessLogic.User(0));
                newSubscriber.getProperty("provider").Value = provider;
                newSubscriber.getProperty("gender").Value = gender;
                newSubscriber.getProperty("location").Value = locaction;
                newSubscriber.getProperty("fbauthorize").Value = true;
                newSubscriber.Save();
                Log.Add(LogTypes.Notify, -1, "New User Created: " + loginName);
                return newSubscriber;
                
            }
            catch (Exception ex)
            {
                UmbHelper.WriteExceptionLog("UserManager.CreateSubscriber()", ex);               
            }
            return null;
            
        }

        public static bool SubscriberExists(string loginName)
        {
            bool result = false;

            return result;
        }

        public static void DeAuthorizeFacebookSubscriber(string fbUserId)
        {
            if (fbUserId.IsNotNullAndNotEmpty())
            {
                fbUserId = FACEBOOK_PREFIX + fbUserId.Trim();
                if (Member.IsMember(fbUserId))
                {
                    Member m = Member.GetMemberFromLoginName(fbUserId);
                    if(m != null)
                    {
                        m.getProperty("fbauthorize").Value = false;
                        m.Save();
                    }
                }
            }
        }

        public static void UpdateFBAuthorizeStatus(Member facebookSubscriber)
        {
            try
            {
                if (facebookSubscriber != null)
                {
                    bool currentAuthorizeStatus = (bool)facebookSubscriber.getProperty("fbauthorize").Value;
                    if (currentAuthorizeStatus == false)
                    {
                        facebookSubscriber.getProperty("fbauthorize").Value = true;
                        facebookSubscriber.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                UmbHelper.WriteExceptionLog("Error in UserManager.UpdateFBAuthorizeStatus()", ex);
            }
        }
    }
}
